/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.hiran.safewalker.canvas;

import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.midlet.MIDlet;

import com.hiran.safewalker.helper.WalkerSprite;

public class SafeWalkerGameCanvas extends GameCanvas implements Runnable
{
    final int MAX_WALKERS = 15;
    boolean gameRunning = false;
    private int width;
    private int height;
    int y = 0;
    int x = 0;
    Image offScreen;
    Sprite playerSprite;
	boolean isPlayerWalking = false;
	int playerSpeed = 5;
	WalkerSprite[] walkerSprite = new  WalkerSprite[MAX_WALKERS];;
	int walterSpriteCount = 0;
	String msg = "";
	private String player_tile_img = "/com/hiran/safewalker/images/player_tiles.png";
	MIDlet parent;
	int boarderSize = 20;
	
    public SafeWalkerGameCanvas(MIDlet parent)
    {
		super(true);
		width = getWidth();
		height = getHeight();
		x = width/2;
		y = height-10;

		this.parent = parent;
    }

	public void createNewWalterSprite()
	{
		if (walterSpriteCount>=MAX_WALKERS) return;
		try {
			walkerSprite[walterSpriteCount] = new WalkerSprite(Image.createImage(player_tile_img), 16, 16);
			walkerSprite[walterSpriteCount].setMaxWidth(width);
			walkerSprite[walterSpriteCount].setMaxHeight(height-50);
			walterSpriteCount++;
		} catch (IOException ex) {
			ex.printStackTrace();
		}

	}

    public void start()
    {
		gameRunning = true;
		try {
			playerSprite = new Sprite(Image.createImage(player_tile_img), 16, 16);
			playerSprite.defineReferencePixel(playerSprite.getWidth()/2, playerSprite.getHeight());
			playerSprite.setFrameSequence(new int[]{8,9,10,11});


		} catch (IOException ex) {
			ex.printStackTrace();
		}

		Thread gameThread = new Thread(this);
		gameThread.start();
    }

    public void stop() { gameRunning = false; }

    public void run()
    {
		Graphics g = getGraphics();

		while (gameRunning) //is true
		{
			verifyGameState();
			checkUserInput();
			updateGameScreen(g);

			flushGraphics();
			try
			{
			Thread.sleep(20 );
			}catch (InterruptedException ie) { stop(); }
		}
    }
    private void verifyGameState()
    {
		//Nothing yet
    }

    private void checkUserInput()
    {
		int keyState = getKeyStates();
		if (keyState==UP_PRESSED)
		{
			if ( y>playerSpeed )y -= playerSpeed;
			isPlayerWalking = true;
		}
		if (keyState==DOWN_PRESSED)
		{
			if ( y<height )y += playerSpeed;
			isPlayerWalking = true;
		}

		if (keyState==LEFT_PRESSED)
		{
			if ( x>playerSpeed )x -= playerSpeed;
			isPlayerWalking = true;
		}

		if (keyState==RIGHT_PRESSED)
		{
			if ( x<width-playerSpeed ) x += playerSpeed;
			isPlayerWalking = true;
		}
    }

    private void updateGameScreen(Graphics g)
    {
		offScreen = Image.createImage(width,height);
		updateWalkers();
		updatePlayer();
		if (checkCollision()) msg = "You Lost!";
		showMessage();
		drawBoarders();
		g.drawImage(offScreen, 0, 0, g.TOP | g.LEFT);
    }

	private void drawBoarders()
	{
		Graphics g = offScreen.getGraphics();
		g.drawRect(boarderSize, boarderSize, width-(boarderSize*2), height-(boarderSize*3));
	}
	private void updateWalkers()
	{
		Graphics g = offScreen.getGraphics();
		createNewWalterSprite();
		for (int i=0;i<walterSpriteCount;i++)
		{
			walkerSprite[i].nextStep();
			walkerSprite[i].paint(g);
		}
	}

    public void updatePlayer()
    {
		Graphics g = offScreen.getGraphics();
		playerSprite.setRefPixelPosition(x, y);
		if (isPlayerWalking)
		{
			playerSprite.nextFrame();
			isPlayerWalking = false;
		}
		playerSprite.paint(g);
    }

	public boolean checkCollision()
	{
		for (int i=0;i<walterSpriteCount;i++)
		{
			int walkerX = walkerSprite[i].x;
			int walkerY = walkerSprite[i].y;
			if ( Math.abs(x - walkerX)<10 && Math.abs(y - walkerY)<10 ) return true;
		}
		return false;
	}

	public void showMessage()
    {
		Graphics g = offScreen.getGraphics();
		g.drawString(msg, width/2, height/2, g.TOP | g.LEFT);
    }
}